#include "imageRecallTrick.h"


const char *faces[] = { "deuce", "three", "four", "five",
  "six", "seven", "eight", "nine", "ten",
  "jack", "queen", "king" , "ace" };

/* initialize array of pointers */
const char *suits[] = { "heart", "dimnd", "clubs", "spade"};




//--------------------------------------------------------------
void imageRecallTrick::setup(){
	
	sprintf(name, "eye");
	bUseBetterHandMasking = false;
	suit = 0;
	card = 0;
	font.loadFont("fonts/cour.ttf",30);
	io.allocate(width, height, OF_IMAGE_COLOR);
	bSave = false;
	mask.allocate(width, height);
	
	cardWithMask.allocate(width, height, GL_RGBA);
	cardWithMaskPixels = new unsigned char [width*height*4];
	
	bTakeSnapshot 					= false;
	bHaveBlobs 						= false;
	bShowSnapshot					= false;
	
	
	
	
	//-------------------------------------------------- load the card:
	int cardNum = suit * 13 + card;
	char temp[255];
	sprintf(temp, "images/cards/card_%i.png", cardNum);
	cardImage.loadImage(temp);
	
	sprintf(temp, "images/masks/card_%i.png", cardNum);
	maskImage.loadImage(temp);
	maskImage.setImageType(OF_IMAGE_GRAYSCALE);
	
	if (cardImage.width > 0 && cardImage.height > 0){
		unsigned char * colorPixels = cardImage.getPixels();
		unsigned char * maskPixels = maskImage.getPixels();
		for (int i = 0; i < width; i++){
			for (int j = 0; j < height; j++){
				int pos = j*width + i;
				cardWithMaskPixels[pos*4+0]=colorPixels[pos*3+0];
				cardWithMaskPixels[pos*4+1]=colorPixels[pos*3+1];
				cardWithMaskPixels[pos*4+2]=colorPixels[pos*3+2];
				cardWithMaskPixels[pos*4+3]=maskPixels[pos];
			}
		}
		cardWithMask.loadData(cardWithMaskPixels, width, height, GL_RGBA);
		//-------------------------------------------------
	}
}

//--------------------------------------------------------------
void imageRecallTrick::update(){
	
	
	if (bTakeSnapshot == true){
		printf("snapshot \n");
		vbd->takeSnapshot();
		if (vbd->nBlobs > 0){
			printf("%i \n", vbd->nBlobs);
			memcpy(&blob,&vbd->vidBlob, sizeof(videoBlob));
			bHaveBlobs = true;
		}
		bTakeSnapshot = false;
	}
	
	if (bHaveBlobs == true){
		blob.update();
	}

}

//--------------------------------------------------------------
void imageRecallTrick::draw(){
	
	
	
	
	if (bSave == true){
		
		mask.set(0);
		int nBlobs = vbd->contourFinder.nBlobs;
		for (int i = 0; i < nBlobs; i++){
			mask.draw(vbd->contourFinder.blobs[i], 255);
		}
		
		
		video->draw(0,0);
		io.grabScreen(0,0,width, height);
		int cardNum = suit * 13 + card;
		char temp[255];
		sprintf(temp, "images/cards/card_%i.png", cardNum);
		io.saveImage(temp);
		
		mask.draw(0,0);
		io.grabScreen(0,0,width, height);
		sprintf(temp, "images/masks/card_%i.png", cardNum);
		io .saveImage(temp);
		
		bSave = false;
	}
	
	
	showPct = 0.95f * showPct + 0.05f * showPctTarget; 
	snapShotPct = 0.95f * snapShotPct + 0.05f * snapShotPctTarget; 
			
				
	if (showPct > 0.01f){
		//cardImage.draw(0,0,width*3, height*3);
		ofEnableAlphaBlending();
		ofSetColor(255,255,255,showPct*255);
		cardWithMask.draw(0,0,width*3, height*3);
		ofDisableAlphaBlending();
	}
	
	if (snapShotPct > 0.01f){
		if (bHaveBlobs == true){
			ofEnableAlphaBlending();
			ofSetColor(255,255,255,snapShotPct*255);
			blob.draw();
			ofDisableAlphaBlending();
		}
	}
	
	
	char temp[1024];
	sprintf(temp, "(%s - %s) \n", suits[suit], faces[card]);
	if (bDrawDiagnostically){
		ofSetColor(0xffffff);
		font.drawString(temp, 30,30);
	}
	
	if (bDrawDiagnostically){
		// draw the mask of the card :
		ofEnableAlphaBlending();
		ofSetColor(255,255,255,80);
		maskImage.draw(0,0,width*3, height*3);
		ofDisableAlphaBlending();
		
		ofEnableAlphaBlending();
		ofSetColor(255,0,0,140);
		if (bHaveBlobs == true) blob.drawDiagnostically();
		ofDisableAlphaBlending();
	}
	ofSetColor(0xffffff);
}

//--------------------------------------------------------------
void imageRecallTrick::keyPressed(int key){
	
	bool bLoadCard = false;
	switch (key){
		case '1':
			suit--;
			suit=MIN(MAX(suit, 0), 3);
			break;
		case '2':
			suit++;
			suit=MIN(MAX(suit, 0), 3);
			break;
		case '3':
			card--;
			card=MIN(MAX(card, 0), 12);
			break;
		case '4':
			card++;
			card=MIN(MAX(card, 0), 12);
			break;
		case '0':
			bShow = !bShow;
			if (bShow) bLoadCard = true;
			if (bShow){//showPct = 0;
				showPctTarget = 1;
			} else {
				showPctTarget = 0;
			}
			break;
		case '9':
			//bSave = true;
			//bShow = false;
			break;
		case 's':
			bTakeSnapshot = true;
			bShowSnapshot = false;
			
			break;
		case 'l':
			bShowSnapshot = !bShowSnapshot;
			if (bShowSnapshot){  //snapShotPct = 0;
				snapShotPctTarget = 1;		
			} else {
				snapShotPctTarget = 0;		
			}
			
			break;
	}
	
	
	//-------------------------------------------------- load the card:
	int cardNum = suit * 13 + card;
	char temp[255];
	sprintf(temp, "images/cards/card_%i.png", cardNum);
	cardImage.loadImage(temp);
	
	sprintf(temp, "images/masks/card_%i.png", cardNum);
	maskImage.loadImage(temp);
	maskImage.setImageType(OF_IMAGE_GRAYSCALE);
	
	if (cardImage.width > 0 && cardImage.height > 0){
		unsigned char * colorPixels = cardImage.getPixels();
		unsigned char * maskPixels = maskImage.getPixels();
		for (int i = 0; i < width; i++){
			for (int j = 0; j < height; j++){
				int pos = j*width + i;
				cardWithMaskPixels[pos*4+0]=colorPixels[pos*3+0];
				cardWithMaskPixels[pos*4+1]=colorPixels[pos*3+1];
				cardWithMaskPixels[pos*4+2]=colorPixels[pos*3+2];
				cardWithMaskPixels[pos*4+3]=maskPixels[pos];
			}
		}
		cardWithMask.loadData(cardWithMaskPixels, width, height, GL_RGBA);
		//-------------------------------------------------
	}
	
	
	
}